﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Citi.Import.Util;
using Citi.RoutineBase;
using Citi.ResourceConsolidation.Domain;
using System.Globalization;

namespace Citi.ResourceConsolidation.SchedulerUI
{
    public enum ResourceType
    {
        GDW,
        Matrix,
        NEMS
    }

    public class ResourceUpdateDatabase
    {
        public static List<Error> ImportResourceConsolidation(DateTime referenceDate, ICurrentAction currentAction, List<Error> errors, System.Data.DataTable dataTable, ResourceType resourceType, out List<Resource> resources)
        {
            if (resourceType == ResourceType.GDW)
            {
                currentAction.Description = "Filling Location Objects";
                var locations = Conversion.Convert(Location.Definitions, dataTable, ref errors);
                currentAction.Description = string.Format("Saving {0} registers", locations.Count());
                Location.Save(locations);

                //HumanResourceDeptImport.LevelParent = new HumanResourceDeptImport();
                //currentAction.Description = "Filling Human Resource Department Objects";
                //var humanResourceDepartments = Conversion.Convert(HumanResourceDeptImport.Definitions, dataTable, ref errors);
                //HumanResourceDeptImport.Save<HumanResourceDepartmentComparer>(humanResourceDepartments);

                currentAction.Description = "Filling Officer Objects";
                var officers = Conversion.Convert(Officer.Definitions, dataTable, ref errors);
                Officer.Save(officers);

                currentAction.Description = "Filling Operating Objects";
                var operating = Conversion.Convert(Operating.Definitions, dataTable, ref errors);
                Operating.Save(operating);

                currentAction.Description = "Filling Vendor Objects";
                var vendors = Conversion.Convert(Vendor.Definitions, dataTable, ref errors);
                Vendor.Save(vendors);

                currentAction.Description = "Filling Business Contact Objects";
                var businessContacts = Conversion.Convert(BusinessContact.Definitions, dataTable, ref errors);
                BusinessContact.Save(businessContacts);

                currentAction.Description = "Filling Business Unit Objects";
                var businessUnits = Conversion.Convert(BusinessUnit.Definitions, dataTable, ref errors);
                BusinessUnit.Save(businessUnits);

                currentAction.Description = "Filling Human Resource Objects";
                var humanResources = Conversion.Convert(HumanResource.Definitions, dataTable, ref errors);
                HumanResource.Save(humanResources);

                currentAction.Description = "Filling Employee Class Objects";
                var employeeClasses = Conversion.Convert(EmployeeClass.Definitions, dataTable, ref errors);
                EmployeeClass.Save(employeeClasses);

                currentAction.Description = "Filling Job Function Objects";
                var functions = Conversion.Convert(JobFunction.Definitions, dataTable, ref errors);
                JobFunction.Save(functions);

                currentAction.Description = "Filling Job Family Objects";
                var families = Conversion.Convert(JobFamily.Definitions, dataTable, ref errors);
                JobFamily.Save(families);

                currentAction.Description = "Filling Job Title Objects";
                var titles = Conversion.Convert(JobTitle.Definitions, dataTable, ref errors);
                JobTitle.Save(titles);

                currentAction.Description = "Filling Country Objects";
                var countries = Conversion.Convert(DomicileCountry.Definitions, dataTable, ref errors);
                DomicileCountry.Save(countries);
            }

            if (resourceType != ResourceType.Matrix)
                currentAction.Description = "Filling Resource Objects";

            resources = Conversion.Convert(resourceType == ResourceType.GDW ? Resource.GDWDefinition :
                                            resourceType == ResourceType.Matrix ? Resource.MatrixDefinition :
                                            Resource.NEMSDefinition, dataTable, ref errors);

            resources.ForEach(x =>
            {
                x.Active = x.Active ?? "A";
            });

            Resource.Save(resources, "GDW");

            if (resourceType != ResourceType.Matrix)
                currentAction.Description = "Filling Resource Month Objects";

            var resourceMonths = Conversion.Convert(resourceType == ResourceType.GDW ? ResourceMonth.GDWDefinitions :
                                                    resourceType == ResourceType.Matrix ? ResourceMonth.MatrixDefinition :
                                                    ResourceMonth.MatrixDefinition, dataTable, ref errors);

            resourceMonths.ForEach(x =>
            {
                x.LastUpdate = DateTime.Now;
                x.ReferenceDate = referenceDate;
                x.HumanResourceDeptId = x.HumanResourceDeptId ?? "Matrix";
            });

            if (resourceType != ResourceType.Matrix)
                ResourceMonth.Save(resourceMonths);

            currentAction.Description = "Extraction finished";
            return errors;
        }
    }
}
